<?php
function_exists('UnixPath')  || die('OK');


	$ACT = array('list', 'add', 'aok', 'edit', 'eok', 'del');
	in_array($_GET['act'], $ACT) || $_GET['act'] = 'list';
	//$html['tpl_right_act'] = $_GET['act'];

	$_GET['id']	= (int) $_GET['id'];
	$body['navlist'] = array(
		'list' => '数据表列表',
		'add' => '添加数据表',
	);

	$body['_url']	= '&pid='.$P['id'];
	$body['url']	= $html['url'].$body['_url'];
	$body['navslist']	= array();
	//$body['node_tree'] = node_tree($DB, $P, E::$DB->_tab, 'nodeName' ); 
	//E::$DB->_tab = $tab = '_table_list';

	$ShowTypes	= require( B_DIR . '/_res/const/ShowTypes.php');
	$tpl->assign_by_ref('ShowTypes', $ShowTypes);

if( $_GET['act'] === 'aok' ){
	$_POST['pid'] = $P['id'];
	$_POST['nodeType'] = $P['nodeType'] + 1;
	E::$DB->insert('_table_list', 'tableName, itemName, description, isGroup, isForm83');
	tab_cache();
	A::msg('添加数据表 '.$_POST['description'].' 完成!', $_POST['jurl'], 1, 1 );
}

if( $_GET['act'] === 'edit' ){
	$L = E::$DB->one('_table_list','id='.$_GET['id']);
	$L['id'] || A::msg('无效的表', $_POST['jurl'], 1, 1 );
	$body['node_tree'] = '<a href="?mod='.$_GET['mod'].'&fun=table&act=list">数据表列表</a> <small>&#187;</small> <a href="?mod='.$_GET['mod'].'&fun=field&act=list&pid='.$L['id'].'">'.$L['description'].'</a>';

	array_push( $body['navslist'], array('更新表单', '?mod='.$_GET['mod'].'&fun=field&act=form&pid=' . $_GET['id']) );
	array_push( $body['navslist'], array('字段管理', '?mod='.$_GET['mod'].'&fun=field&act=list&pid=' . $_GET['id']) );
	if( $L['isGroup'] ){
		array_push( $body['navslist'], array('字段分组', '?mod='.$_GET['mod'].'&fun=field_group&act=list&pid=' . $_GET['id']) );
	}

	$Fields	= E::$DB->ary('_table_field', 'tid=' . $L['id'] .' ORDER BY gid ASC, orderId DESC , id ASC' , 'id, fieldName, gid', 'id' );
	$tpl->assign_by_ref('Fields', $Fields);
	$Groups	= E::$DB->ary('_table_group', 'tid=' . $L['id'] .' ORDER BY iOrder DESC , id ASC' , 'id, name', 'id' );
	$tpl->assign_by_ref('Groups', $Groups);
	
	$L['ShowInfo']	= @unserialize($L['ShowInfo']);
	
}

if( $_GET['act'] === 'eok' ){
	$L = E::$DB->one('_table_list', 'id='.$_GET['id']);
	$L['id'] || A::msg('无效的表', $_POST['jurl'], 1, 1 );
	$_POST['isGroup']	= (int) $_POST['isGroup'];

	E::$DB->update('_table_list', ' tableName, itemName, description, isGroup, isForm83', 'id='.$L['id']);
	if( !$_POST['isGroup'] && $L['isGroup'] ){
		E::$DB->query("UPDATE _table_field SET gid=0 WHERE tid=" . $L['id']);
	}else{

		if( is_array($_POST['gid']) ) {
			$Fields	= E::$DB->ary('_table_field', 'tid=' . $L['id'] .' ORDER BY gid ASC, orderId DESC , id ASC' , 'id,gid,tid', 'id' );
			foreach($_POST['gid'] as $fid => $gid ){
				$fid	= (int) $fid;
				$gid	= (int) $gid;
				if( $Fields[$fid]['tid'] != $L['id'] ){
					continue;
				}
				if( $Fields[$fid]['gid'] == $gid ){
					continue;
				}
				E::$DB->query("UPDATE _table_field SET gid=$gid WHERE id=$fid");
			}
		}
	}
	
	if( is_array( $_POST['si'] ) ) {
		$li	= array();
		foreach( $_POST['si'] as $_fid => $i ){
			$fid	= (int) $_fid;
			if( empty( $ShowTypes[$i]) || $i == 'no' ){
				continue;
			}
			$li[$fid]	= array( $i );
			$x	= (int) $_POST['sx'][$fid];
			$y	= (int) $_POST['sy'][$fid];
			$z	= (int) $_POST['sz'][$fid];
			if( $x || $y|| $z ){
				array_push( $li[$fid], $x);
				array_push( $li[$fid], $y);
				array_push( $li[$fid], $z);
			}
		}
		$si		= @addslashes( @serialize($li) );
		$qstr	= 'UPDATE _table_list SET ShowInfo="' . $si . '" WHERE id=' . $_GET['id'];
		E::$DB->query($qstr);
	}

	tab_cache();
	A::msg('修改表 '.$_POST['description'].' 完成!', $_POST['jurl'], 1, 1 );
}


if( $_GET['act'] === 'del' ){
	$L = E::$DB->one('_table_list', 'id='.$_GET['id']);
	$L['id'] || A::msg('无效的表', $_POST['jurl'], 1, 1 );

	if( $L ['id'] < 3){
		A::msg('你没有权限操作此表！', $_POST['jurl'], 1, 1 );
	}
	
	$x = E::$DB->ary("_table_field", "tid='$L[id]'", "id");
	count($x) && A::msg("字段未清除!", $_POST['jurl'], 1, 1 );

	E::$DB->query("DELETE FROM  _table_list where id='$L[id]'");
	C::tree('_table_list', $order, $cache_file);
	tab_cache();
	A::msg('删除表 '.$L['description'].' 完成!', $_POST['jurl'], 1, 1 );
}


if( $_GET['act'] === 'list' ){
	$p = E::$DB->page('_table_list');
	$L = E::$DB->all('_table_list', '1 '. $p['limit']);


	$rs	= mysql_query('SHOW TABLES',  E::$DB->_handle);

	if( mysql_error() === '' ){
		$Tables	= array();
		while( $table  = mysql_fetch_array($rs)){
			$Tables[$table[0]]	=  true;
		}
		foreach($L as &$v){
			$v['isExists']	=  $Tables[$v['tableName']] ;
		}
		unset($v);
	}else{
		die('Error On File:' . __FILE__ . ' Line:' . __LINE__);
	}
}


function tab_cache(){
	//global $DB;
	$l	= E::$DB->ary('_table_list' , '1 order BY id ASC','*','id');
	hash::save($l, B_DIR . '/_res/const/tabs.php');
}
